package cn.Mmovie.util;

import java.util.ArrayList;
import java.util.List;

/**
 * 分页操作组件
 */
public class PageUtil<T> {
    private Integer pageNo;
    private Integer pageCount;
    private Integer pageSize;
    private Integer recordCount;
    private Integer pageStart;
    private Integer pageEnd;
    private Integer pageLoop = 5;
    private List<T> items = new ArrayList<T>();
    private Boolean hasPrevious;
    private Boolean hasNext;
    private String url;

    public Integer getPageNo() {
        return pageNo;
    }

    public void setPageNo(Integer pageNo) {
        this.pageNo = pageNo;
    }

    public Integer getPageCount() {
        return pageCount;
    }

    public void setPageCount(Integer pageCount) {
        this.pageCount = pageCount;
    }

    public Integer getPageSize() {
        return pageSize;
    }

    public void setPageSize(Integer pageSize) {
        this.pageSize = pageSize;
    }

    public Integer getRecordCount() {
        return recordCount;
    }

    public void setRecordCount(Integer recordCount) {
        this.recordCount = recordCount;
        pageCount = recordCount / pageSize;
        if (recordCount % pageSize > 0) {
            pageCount++;
        }
        if (pageNo < 1) {
            pageNo = 1;
        }
        if (pageNo > pageCount) {
            pageNo = pageCount;
        }
        //计算页码循环范围
        Integer range = (pageLoop + 1) / 2;
        pageStart = pageNo;
        pageEnd = pageNo;
        for (int i = 1; i < range; i++) {
            if (pageStart == 1) {
                break;
            }
            pageStart--;
        }
        for (int i = 1; i < range; i++) {
            if (pageEnd == pageCount) {
                break;
            }
            pageEnd++;
        }
    }

    public Integer getPageStart() {
        return pageStart;
    }

    public void setPageStart(Integer pageStart) {
        this.pageStart = pageStart;
    }

    public Integer getPageEnd() {
        return pageEnd;
    }

    public void setPageEnd(Integer pageEnd) {
        this.pageEnd = pageEnd;
    }

    public Integer getPageLoop() {
        return pageLoop;
    }

    public void setPageLoop(Integer pageLoop) {
        this.pageLoop = pageLoop;
    }

    public Boolean getHasPrevious() {
        return hasPrevious;
    }

    public void setHasPrevious(Boolean hasPrevious) {
        this.hasPrevious = hasPrevious;
    }

    public Boolean getHasNext() {
        return hasNext;
    }

    public void setHasNext(Boolean hasNext) {
        this.hasNext = hasNext;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public List<T> getItems() {
        return items;
    }

    public void setItems(List<T> items) {
        this.items = items;
    }

    @Override
    public String toString() {
        return "PageUtil{" +
                "pageNo=" + pageNo +
                ", pageCount=" + pageCount +
                ", pageSize=" + pageSize +
                ", recordCount=" + recordCount +
                ", pageStart=" + pageStart +
                ", pageEnd=" + pageEnd +
                ", pageLoop=" + pageLoop +
                ", items=" + items +
                ", hasPrevious=" + hasPrevious +
                ", hasNext=" + hasNext +
                ", url='" + url + '\'' +
                '}';
    }
}
